<?php

namespace core;
/**
 * Config class
 *
 */
class Config
{
	/************************************************************
	*   					Properties							*
	************************************************************/


	/**
	 * Configs array
	 *
	 * @var	array
	 */
	protected $configs = array();




	/************************************************************
	*   					*structors							*
	************************************************************/


	function __construct()
	{
	}

	function __destruct()
	{
	}



	/************************************************************
	*   					Private methods						*
	************************************************************/


	/************************************************************
	*   					Public methods						*
	************************************************************/


	/**
	 * Fetches configs and sets them up
	 *
	 * @param	string	$configName		Name of config to load
	 */
	private function SetConfig($configName)
	{
		//Base config
		if ($configName == "main")
		{
			$configs = service("db")->
			Select("*")->
			Distinct()->
			From("_config")->
			Run();

			if (empty($configs))
			{
				service("debug")->error("No configs found");
			}

			$type = core("User")->GetUserType();

			foreach ($configs as $item)
			{
				if ($item['user_type'] == $type['id'])
				{
					$config = $item;
					break;
				}
			}

			if (!isset($config))
			{
				service("debug")->error("No configs found for user type $type[type]");
			}
		}
		//Service config
		else
		{
			$file = _CONFIGS_DIR . $configName . ".config.php";

			$config = core("File")->ImportScript($file, true, false);

			$config = $this->configs[$configName] = $config;
		}

		$this->configs[$configName] = $config;
	}

	/**
	 * Returns config array by its name
	 *
	 * @param	string	$configName		Config name
	 * @return	array					Config array
	 */
	public function GetConfig($configName = "main")
	{
		if (!isset($this->configs[$configName]))
		{
			$this->SetConfig($configName);
		}

		return $this->configs[$configName];
	}

	/**
	 * Returns certain config value
	 *
	 * @param	string	$value			Config value name
	 * @param	string	$configName		Config name
	 * @return 	mixed					Config value
	 */
	public function GetConfigValue($value, $configName = "main")
	{
		$config = $this->GetConfig($configName);

		return $config[$value];
	}
}